package com.spring.mvc.service;

import com.spring.mvc.dao.PersonDao;
import com.spring.mvc.entity.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

/**
 * ${DESCRIPTION}
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2018-02-01 12:31
 **/
@Service
public class PersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    private PersonDao personDao;

    @Autowired
    private SchoolService schoolService;

    /**
     * 根据ID查询
     *
     * @param id 主键
     */
    public Person findPerson(int id) throws SQLException {
        LOGGER.debug("Service 开始执行 dao");
        Person person = personDao.selectById(id);
        LOGGER.debug("Service 执行完毕 dao");
        return person;
    }

    /**
     * 查询Person
     *
     * @param name 名字
     */
    public void savePerson(String name) throws SQLException {
        personDao.insertPerson(name);
    }

    /**
     * 测试事务
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveTestTransactional() throws SQLException {
        this.findPerson(1);
        schoolService.findSchool(1);

        for (int i = 0; i < 3; i++) {
            personDao.insertPerson("test" + i);
        }
        int i = 1 / 0;
    }


}
